package com.totcy.atom.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.totcy.atom.view.fragment.BaseFragment;

import java.util.List;

/**
 * description : 通用的 fragmentAdapter
 *
 * @author : Totcy
 * @date : 2018/2/8 14:43
 * @update : 2018/2/8
 */

public class CommonFragmentAdapter extends FragmentPagerAdapter {

    private List<BaseFragment> fragments;

    public CommonFragmentAdapter(FragmentManager fm, List<BaseFragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment;
        try {
            fragment = fragments.get(position);
        } catch (Exception e) {
            e.printStackTrace();
            fragment = new Fragment();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return ((BaseFragment) getItem(position)).getFragmentTitle();
    }
}
